﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Web;
using System.IO;
using ShopModel;

namespace ShopDAO
{
    public class ErrorDAO
    {
        private string connectionString;

        private SqlConnection Connection()
        {
            return new SqlConnection(connectionString);
        }

        public ErrorDAO()
        {
            connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["csShop"].ConnectionString;
        }

        /// <summary>
        /// Dodaje informację o błędzie do bazy danych (Adds information about the error that has occured to the database.)
        /// </summary>
        /// <param name="ex">Treśc informacj o błędzie która ma zostać zapisana w bazie danyc(Announcement to be added to the database.)</param>
        public void LogError(Exception ex)
        {
            if (ex != null)
            {
                int exeptionId = 0;
                SqlConnection connection = Connection();
                SqlCommand command = connection.CreateCommand();
                command.CommandType = System.Data.CommandType.StoredProcedure;
                command.CommandText = "LogError";
                command.Parameters.Add(new SqlParameter("@message", ex.Message));
                command.Parameters.Add(new SqlParameter("@call_stack", ex.StackTrace));
                command.Parameters.Add(new SqlParameter("@parent_exception", DBNull.Value));
                if (!string.IsNullOrEmpty(ex.Source))
                {
                    command.Parameters.Add(new SqlParameter("@source", ex.Source));
                }

                try
                {
                    connection.Open();
                    exeptionId = Convert.ToInt32(command.ExecuteScalar());


                    while (ex.InnerException != null)
                    {
                        ex = ex.InnerException;
                        command.Parameters["@message"].Value = ex.Message;
                        command.Parameters["@call_stack"].Value = ex.StackTrace;
                        command.Parameters["@parent_exception"].Value = exeptionId;

                        exeptionId = Convert.ToInt32(command.ExecuteScalar());
                    }
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
        }


    }
}
